package network.tpa1;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import network.AbstractNetworkInterface;
import network.AbstractUserSignal;
import network.ListenerBindException;
import network.SendSignalException;
import network.config.NetworkConfiguration;

import controller.MessageController;
import controller.UserNotConnected;

import signals.* ;

public class NetworkInterfaceA1 extends AbstractNetworkInterface {
	
	public void sendHello (String username) throws SendSignalException {
		super.sender.sendSignal(new UserSignalA1(new Hello(username), null)) ;
	}
	
	public void sendHelloReply (String username, InetAddress remoteAddress) throws SendSignalException {
		super.sender.sendSignal(new UserSignalA1(new HelloReply (username), remoteAddress)) ;
	}
	
	public void sendGoodBye () throws SendSignalException {
		super.sender.sendSignal(new UserSignalA1(new GoodBye (), null)) ;
	}
	
	public void sendAskLogin (InetAddress to) throws SendSignalException {
		super.sender.sendSignal(new UserSignalA1(new AskLogin(), to)) ;
	}
	
	public void sendMessage (String message, InetAddress to, InetAddress[] cc) throws SendSignalException {
		super.sender.sendSignal(new UserSignalA1(new SendText(message, cc), to)) ;
	}
	
	public void sendPropFile (String filename, long fileSize, int fileID, InetAddress to) throws SendSignalException {
		super.sender.sendSignal(new UserSignalA1(new PropFile(filename, fileSize, fileID), to)) ;
	}
	
	public void sendAcceptFile(InetAddress to, int fileID, boolean accepted, boolean now) throws SendSignalException {
		super.sender.sendSignal(new UserSignalA1(new AcceptFile(fileID, accepted, now), to)) ;
	}
		
	/**
	 * Create a new NetworkInterface connected with the specified MessageController
	 * 
	 * @param msgCtrl
	 * @throws ListenerBindException if an error occurs starting the udp listener thread
	 */
	public NetworkInterfaceA1 (MessageController msgCtrl, NetworkConfiguration nc) throws ListenerBindException {
		super (msgCtrl, nc) ;
	}
	

	@Override
	protected void addSignal(DatagramPacket packet) {
		addAbstractSignal(new UserSignalA1(packet)) ;
	}
	
	/**
	 * Perform action corresponding to the specified signal using
	 * the message controller.
	 * 
	 * @param si
	 * @throws UserNotConnected 
	 * @throws SendSignalException 
	 * @throws UnknownHostException 
	 */
	protected void performSignal (AbstractUserSignal aus) throws UnknownHostException, SendSignalException, UserNotConnected {
		UserSignalA1 usi = (UserSignalA1)aus ;
		InetAddress addr = usi.getAddress() ;
		Signal si = usi.getSignal() ;
		switch (SignalConversion.getSignalType(si)) {
		case HELLO:
			Hello he = (Hello)si ;
			super.msgCtrl.processHello (he.getUsername(), addr) ;
			break ;
		case HELLO_REPLY:
			HelloReply her = (HelloReply)si ;
			super.msgCtrl.processHelloReply (her.getUsername(), addr) ;
			break ;
		case ASK_LOGIN:
			super.msgCtrl.processAskLogin (addr) ;
			break ;
		case GOOD_BYE:
			super.msgCtrl.processGoodBye (addr) ;
			break ;
		case SEND_TEXT:
			SendText st = (SendText)si ;
			super.msgCtrl.processSendText (st.getMessage(), st.getReceivers(), addr) ;
			break ;
		case ACCEPT_FILE:
			AcceptFile af = (AcceptFile)si ;
			super.msgCtrl.processAcceptFile(af.getFileID(), af.accepted(), af.now(), addr) ;
			break ;
		case PROP_FILE:
			PropFile pf = (PropFile)si ;
			super.msgCtrl.processPropFile(pf.getFileName(), pf.getFileID(), pf.getFileSize(), addr) ;
		default:
			break;
		}
	}
	
}
